package elitech.vietnam.weatherfashion.tabs.settings;

import java.io.IOException;
import java.util.ArrayList;

import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import elitech.vietnam.weatherfashion.R;
import elitech.vietnam.weatherfashion.WFApplication;
import elitech.vietnam.weatherfashion.config.GlobalValue;
import elitech.vietnam.weatherfashion.dto.FriendDTO;
import elitech.vietnam.weatherfashion.dto.MemberDTO;
import elitech.vietnam.weatherfashion.service.PHPSoapServiceCaller;

public class FriendListTabFragment extends Fragment {

	private final String LOG_TAG = "FriendListTabFragment";
	private ArrayList<FriendDTO> listFriend;
	private ArrayList<FriendDTO> listFriendTuiAdd = new ArrayList<FriendDTO>();
	private ArrayList<FriendDTO> listFriendAddTui = new ArrayList<FriendDTO>();

	private ProgressDialog mProgressDialog;
	private ListView lvFriendList;
	private AdapterFriendListListView adapterFiendList;
	private int typeListFriend = 0;
	private boolean isRead = false;
	private ImageView image_nodata;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		if (getArguments() != null) {
			typeListFriend = getArguments().getInt("list_friend");
		}

		View view = inflater.inflate(R.layout.fragment_friend_list_tab,
				container, false);

		initView(view);
		return view;
	}

	@Override
	public void onResume() {
		super.onResume();
		getActivity().registerReceiver(deletedReceiver,
				new IntentFilter(AdapterFriendListListView.BROADCAST_DELETED));
	}

	@Override
	public void onStop() {
		super.onStop();
		getActivity().unregisterReceiver(deletedReceiver);
	}

	BroadcastReceiver deletedReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			boolean deleted = intent.getBooleanExtra("DELETE_DONE", false);
			Log.i(LOG_TAG, "onReceive broadcast + " + deleted);
			if (deleted) {
				if (typeListFriend == 1) {
					listFriendTuiAdd = adapterFiendList.getList();
					GlobalValue.getInstance(getActivity()).writeFriendTuiAdd(
							listFriendTuiAdd);
					if (listFriendTuiAdd.size() == 0) {
						lvFriendList.setVisibility(View.INVISIBLE);
						image_nodata.setVisibility(View.VISIBLE);
					}
				} else if (typeListFriend == 2) {
					listFriendAddTui = adapterFiendList.getList();
					GlobalValue.getInstance(getActivity()).writeFriendAddTui(
							listFriendAddTui);
					if (listFriendAddTui.size() == 0) {
						lvFriendList.setVisibility(View.INVISIBLE);
						image_nodata.setVisibility(View.VISIBLE);
					}
				}
			}
		}
	};

	private void initView(View view) {

		/********************************/
		mProgressDialog = new ProgressDialog(getActivity());
		mProgressDialog.setTitle(this.getResources().getString(
				R.string.setting_friend_list_title_process));
		mProgressDialog.setMessage(this.getResources().getString(
				R.string.setting_change_password_processing));
		mProgressDialog.setCancelable(false);

		/******************************************/
		lvFriendList = (ListView) view.findViewById(R.id.lvFriendList);
		image_nodata = (ImageView) view.findViewById(R.id.image_nodata);
		/*******************************/

		if (typeListFriend == 1 && !isRead) {
			MemberDTO user = GlobalValue.getInstance(getActivity())
					.readUserInfo();
			LoadListFriendAsynTask loadListFriendAsynTask = new LoadListFriendAsynTask();
			loadListFriendAsynTask.execute(user.getId());
			isRead = true;
		} else if (typeListFriend == 1 && isRead) {
			listFriendTuiAdd = GlobalValue.getInstance(getActivity())
					.readFriendTuiAdd();
			if (listFriendTuiAdd == null)
				listFriendTuiAdd = new ArrayList<FriendDTO>();

			reloadListFriend(listFriendTuiAdd, false);

		} else if (typeListFriend == 2) {
			listFriendAddTui = GlobalValue.getInstance(getActivity())
					.readFriendAddTui();
			if (listFriendAddTui == null)
				listFriendAddTui = new ArrayList<FriendDTO>();

			reloadListFriend(listFriendAddTui, true);
		}
	}

	private void reloadListFriend(ArrayList<FriendDTO> friendList, boolean b) {
		if (friendList.size() > 0) {
			image_nodata.setVisibility(View.INVISIBLE);
			lvFriendList.setVisibility(View.VISIBLE);
			adapterFiendList = new AdapterFriendListListView(getActivity(),
					friendList, b);
			lvFriendList.setAdapter(adapterFiendList);
		} else {
			lvFriendList.setVisibility(View.INVISIBLE);
			image_nodata.setVisibility(View.VISIBLE);
		}
	}

	private void tachFriendList() {

		listFriendAddTui.clear();
		listFriendTuiAdd.clear();

		if (listFriend != null)
			for (FriendDTO friendDTO : listFriend) {
				if (friendDTO.getType() == 1) {
					listFriendTuiAdd.add(friendDTO);
				} else {
					listFriendAddTui.add(friendDTO);
				}
			}

		GlobalValue.getInstance(getActivity()).writeFriendTuiAdd(
				listFriendTuiAdd);
		GlobalValue.getInstance(getActivity()).writeFriendAddTui(
				listFriendAddTui);
	}

	private class LoadListFriendAsynTask extends
			AsyncTask<Integer, Integer, ArrayList<FriendDTO>> {

		@Override
		protected ArrayList<FriendDTO> doInBackground(Integer... params) {

			int userId = params[0];

			PHPSoapServiceCaller serviceCaller = new PHPSoapServiceCaller();
			try {
				listFriend = serviceCaller.getListFriend(userId);
			} catch (IOException e) {
				e.printStackTrace();
				if (listFriend == null)
					listFriend = new ArrayList<FriendDTO>();
			}

			// ArrayList<FriendDTO> abc;
			// GlobalValue.getInstance(getActivity()).writeFriendList(listFriend);
			// abc = GlobalValue.getInstance(getActivity()).readFriendList();

			return listFriend;
		}

		@Override
		protected void onPostExecute(ArrayList<FriendDTO> result) {
			super.onPostExecute(result);
			mProgressDialog.dismiss();
			WFApplication.getInstance().userFriends = result;
			listFriend = result;
			tachFriendList();
			reloadListFriend(listFriendTuiAdd, false);
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			mProgressDialog.show();
		}
	}
}
